'''
Created on Aug 25, 2011

@author: david
'''
from pspy.gae.actiondomain import PspyAction


class Buy(PspyAction):
    """
    Default Action 
    spawns index.gen
    """
    def execute(self): 
        from app.Ranger.agents import AppAgent
        from pspy import Notification
        import json
        import logging

        req = self.request
        rslts = {'code': 200, "message": "Dealer has been notified and will be contacting you shortly"}

        # extract data
        data = {"did": req.getParameter("did"),
                "vin": req.getParameter("vin"),
                "pin": req.session[req.getParameter("did")],
                "descr": req.getParameter("descr"),
                "pymts": req.getParameter("pymts")
                }

        logging.info("~~ Buy It: %s" % str(data))

        try:
            # send to agent for dealer notification
            AppAgent().buyIt(data)

        except Exception as ex:
            Notification("Notification Failure").setException(ex)
            rslts = {"code": 500, "message": "Error Sending Notification. Please try again later."}

        return self.JSONOutput(json.dumps(rslts))

    def sess_check(self):
        return True